//
//  LJJLoginView.m
//  shopTest
//
//  Created by 梁佳佳 on 2016/11/14.
//  Copyright © 2016年 梁佳佳. All rights reserved.
//

#import "LJJLoginView.h"

@interface LJJLoginView () <UITextFieldDelegate>

@property (strong, nonatomic) UITextField *userNameText;    /** 手机号text */
@property (strong, nonatomic) UITextField *passwordText;    /** 密码text */
@property (strong, nonatomic) UIButton *loginBtn;    /** 登录 */
@property (strong, nonatomic) UIButton *registBtn;    /** 注册 */
@property (strong, nonatomic) UILabel *textBackLabel;     /** 输入框背景图 */
@property (strong, nonatomic) UIView *textLineView;    /** text中间的分割线 */

@end

@implementation LJJLoginView
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self addSubview:self.textBackLabel];
        [self addSubview:self.userNameText];
        [self addSubview:self.passwordText];
        [self addSubview:self.textLineView];
        [self addSubview:self.loginBtn];
        [self addSubview:self.registBtn];
    }
    return self;
}
- (void)layoutSubviews{
    [super layoutSubviews];
    __weak typeof (self) weakSelf = self;
    [_textBackLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@89);
        make.top.equalTo(weakSelf.mas_top).offset(15);
        make.left.equalTo(weakSelf.mas_left).offset(-1);
        make.right.equalTo(weakSelf.mas_right).offset(1);
    }];
    [_userNameText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(weakSelf).offset(15);
        make.height.equalTo(@44);
        make.top.equalTo(weakSelf.textBackLabel.mas_top);
    }];
    [_passwordText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(weakSelf).offset(15);
        make.height.equalTo(@44);
        make.top.equalTo(weakSelf.userNameText.mas_bottom).offset(1);
    }];
    [_textLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@1);
        make.left.equalTo(weakSelf.textBackLabel.mas_left).offset(15);
        make.right.equalTo(weakSelf.textBackLabel.mas_right).offset(-15);
        make.centerY.equalTo(weakSelf.textBackLabel.mas_centerY);
    }];
    [_loginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.mas_left).offset(16);
        make.right.equalTo(weakSelf.mas_right).offset(-16);
        make.height.equalTo(@35);
        make.top.equalTo(weakSelf.passwordText.mas_bottom).offset(15);
    }];
    [_registBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(70, 16));
        make.right.equalTo(weakSelf.mas_right).offset(-15);
        make.top.equalTo(weakSelf.loginBtn.mas_bottom).offset(23);
    }];
}
#pragma mark -
#pragma mark - method
//用户名修改内容时调用的方法
- (void)userNameTextChangeText:(UITextField *)textfield{
    if (textfield.text.length == 11 && _passwordText.text.length > 5) {
        _loginBtn.backgroundColor = RGB(56, 166, 241,1 );
        _loginBtn.userInteractionEnabled = YES;
        _loginBtn.selected = YES;
    }else{
        _loginBtn.backgroundColor = RGB(229, 229, 229, 1);
        _loginBtn.userInteractionEnabled = NO;
        _loginBtn.selected = NO;
    }
}
//密码输入框修改内容是调用的方法
- (void)passwordTextChangeText:(UITextField *)textfield{
    
    if (textfield.text.length > 5 && _userNameText.text.length == 11) {
        _loginBtn.backgroundColor = RGB(56, 166, 241, 1);
        _loginBtn.userInteractionEnabled = YES;
    }else{
        _loginBtn.backgroundColor = RGB(229, 229, 229, 1);
        _loginBtn.userInteractionEnabled = NO;
    }
}
- (void)loginMethod:(UIButton *)sender{
    if(_loginBlock){
        _loginBlock(@{@"LoginName":_userNameText.text,@"Lpassword":_passwordText.text});
    }
}
#pragma mark -
#pragma mark - textField delegate
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    if (textField == self.userNameText && range.location > 10) {
        return NO;
    }
    return YES;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    return [textField resignFirstResponder];
}
#pragma mark -
#pragma mark - lazy method
- (UILabel *)textBackLabel{
    if (!_textBackLabel) {
        _textBackLabel = [[UILabel alloc]init];
        _textBackLabel.backgroundColor = [UIColor whiteColor];
        _textBackLabel.layer.borderWidth = 1;
        _textBackLabel.layer.borderColor = RGB(188, 188, 188, 1).CGColor;
    }
    return _textBackLabel;
}
- (UITextField *)userNameText{
    if (!_userNameText) {
        _userNameText = [[UITextField alloc]init];
        _userNameText.delegate = self;
        _userNameText.placeholder = @"请输入手机号码";
        _userNameText.keyboardType = UIKeyboardTypePhonePad;
        [_userNameText addTarget:self action:@selector(userNameTextChangeText:) forControlEvents:(UIControlEventEditingChanged)];//编辑改变时调用
    }
    return _userNameText;
}
- (UITextField *)passwordText{
    if (!_passwordText) {
        _passwordText = [[UITextField alloc]init];
        _passwordText.delegate = self;
        _passwordText.placeholder = @"请输入密码";
        _passwordText.secureTextEntry = YES;
        [_passwordText addTarget:self action:@selector(passwordTextChangeText:) forControlEvents:(UIControlEventEditingChanged)];
    }
    return _passwordText;
}
- (UIView *)textLineView{
    if (!_textLineView) {
        _textLineView = [[UIView alloc]init];
        _textLineView.backgroundColor = RGB(188, 188, 188, 1);
    }
    return _textLineView;
}
- (UIButton *)loginBtn{
    if (!_loginBtn) {
        _loginBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
        [_loginBtn setTitle:@"登录" forState:(UIControlStateNormal)];
        _loginBtn.backgroundColor = RGB(229, 229, 229, 1);
        _loginBtn.userInteractionEnabled = NO;//取消用户的交互
        [_loginBtn setTitleColor:RGB(132, 132, 132, 1) forState:(UIControlStateNormal)];
        [_loginBtn setTitleColor:[UIColor whiteColor] forState:(UIControlStateSelected)];
        _loginBtn.selected = NO;
        
        [_loginBtn addTarget:self action:@selector(loginMethod:) forControlEvents:(UIControlEventTouchUpInside)];
    }
    return _loginBtn;
}
- (UIButton *)registBtn{
    if (!_registBtn) {
        _registBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
        [_registBtn setTitle:@"去注册" forState:(UIControlStateNormal)];
        [_registBtn setTitleColor:RGB(56, 145, 241, 1) forState:UIControlStateNormal];
        _registBtn.backgroundColor = MAINCOLOR;
    }
    return _registBtn;
}

@end
